package 面试题;

public class 顺序打印123 {
    private static volatile boolean one = false;
    private static volatile boolean two = false;


    public static void main(String[] args) {
        Runnable one = new Print("one");
        Runnable two = new Print("two");
        Runnable third = new Print("third");

        Foo foo = new Foo();

        new Thread(() ->{
            foo.printSec(two);
        }).start();

        new Thread(() ->{
            foo.printThr(third);
        }).start();

        new Thread(() ->{
            foo.printFir(one);
        }).start();

    }

    static class Print implements Runnable{
        String content;

        public Print(String content) {
            this.content = content;
        }

        @Override
        public void run() {
            System.out.println(content);
        }
    }

    static class Foo {
        public Foo() {

        }

        void  printFir(Runnable runnable){
            runnable.run();
            one = true;
        }

        void printSec(Runnable runnable){
            while (!one);
            runnable.run();
            two = true;
        }

        void printThr(Runnable runnable){
            while (!two);
            runnable.run();
        }
    }

}
